package com.company;

import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    static ArrayList<StudentScore> studentScores = new ArrayList<>();

    public static void main(String[] args) {
// 初始化测试数据
        initData();

// 遍历输出所有数据
        System.out.println("=== 所有学生成绩 ===");
        showAll();

// 计算语文最高分
        System.out.println("\n=== 语文最高分 ===");
        System.out.println("最高分: " + getMaxLanguageScore());

// 计算数学平均分
        System.out.println("\n=== 数学平均分 ===");
        System.out.println("平均分: " + getAverageMathScore());

// 统计男女生人数
        System.out.println("\n=== 男女生人数统计 ===");
        countGender();

// 按英语成绩排序
        System.out.println("\n=== 按英语成绩排序(从高到低) ===");
        sortByEnglishScore();
        showAll();
    }

    // 初始化测试数据
    public static void initData() {
        studentScores.add(new StudentScore("1001", "张三", "男", 85, 90, 78));
        studentScores.add(new StudentScore("1002", "李四", "男", 78, 85, 92));
        studentScores.add(new StudentScore("1003", "王五", "女", 92, 88, 85));
        studentScores.add(new StudentScore("1004", "赵六", "女", 80, 76, 90));
        studentScores.add(new StudentScore("1005", "钱七", "男", 88, 92, 80));
    }


    // 遍历输出所有数据
    public static void showAll() {
        for (StudentScore score : studentScores) {
            System.out.println(score);
        }
    }





    // 计算语文最高分
    public static int getMaxLanguageScore() {
        int max = 0;
        for (StudentScore score : studentScores) {
            if (score.getLanguage() > max) {
                max = score.getLanguage();
            }
        }
        return max;
    }

    // 计算数学平均分
    public static double getAverageMathScore() {
        if (studentScores.isEmpty()) return 0;

        int sum = 0;
        for (StudentScore score : studentScores) {
            sum += score.getMath();
        }
        return (double) sum / studentScores.size();
    }

    // 统计男女生人数
    public static void countGender() {
        int male = 0, female = 0;
        for (StudentScore score : studentScores) {
            if ("男".equals(score.getSex())) {
                male++;
            } else {
                female++;
            }
        }
        System.out.println("男生人数: " + male);
        System.out.println("女生人数: " + female);
    }

    // 按英语成绩排序(从高到低)
    public static void sortByEnglishScore() {
        studentScores.sort(new Comparator<StudentScore>() {
            @Override
            public int compare(StudentScore s1, StudentScore s2) {
                return Integer.compare(s2.getEnglish(), s1.getEnglish());
            }
        });
    }
}